/*
 * @(#)PostScriptExecutor.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.service.supplement;

import java.util.List;

import javax.script.ScriptException;

import com.nhncorp.usf.core.config.runtime.Method;
import com.nhncorp.usf.core.config.runtime.ScriptInfo;
import com.nhncorp.usf.core.service.ExecutionException;
import com.nhncorp.usf.core.service.ServiceExecutionContext;
import com.nhncorp.usf.core.servlet.ServletContextHolder;
import com.nhncorp.usf.core.util.ScriptUtil;

/**
 * @author Web Platform Development Team.
 */
public class PostScriptExecutor implements PostSupplement {

    /**
     * @param context ServiceExecutionContext
     * @return boolean
     * @throws ExecutionException the ExecutionException
     * @see com.nhncorp.usf.core.service.supplement.PostSupplement#afterExecute(com.nhncorp.usf.core.service.ServiceExecutionContext)
     */
    public boolean afterExecute(ServiceExecutionContext context) throws ExecutionException {
        Method method = context.getMethod();
        List<ScriptInfo> postScripts = method.getPostScriptList();

        if (!postScripts.isEmpty()) {
            Object scriptReturn;
            try {
                scriptReturn = ScriptUtil.invokeScript(ServletContextHolder.getContextRealPath(), postScripts,
                                                       context.getAttribute(context.getMethod().getReturnId()));
            } catch (ScriptException e) {
                throw new ExecutionException(e);
            } catch (NoSuchMethodException e) {
                throw new ExecutionException(e);
            }
            context.setAttribute(method.getReturnId(), scriptReturn);
        }

        return true;
    }
}
